package com.yawn.demo;

import com.jvm123.excel.in.ExcelImporter;
import com.jvm123.excs.service.ExExporter;
import com.jvm123.excs.service.ExImporter;
import com.jvm123.excs.service.ExTplExporter;
import com.yawn.demo.entity.Company;
import com.yawn.demo.entity.GradeItem;
import com.yawn.demo.entity.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author yawn
 * 2019/8/7 15:22
 */
@RestController
public class TestController {

    @Autowired
    private ExImporter exImporter;
    @Autowired
    private ExcelImporter excelImporter;
    @Autowired
    private ExExporter exExporter;
    @Autowired
    private ExTplExporter exTplExporter;

    @GetMapping("test1")
    public List<Person> person() {
        List<Person> peo = exImporter.convert("./企业人员汇总表.xlsx", "person", Person.class);
        List<String> name = excelImporter.getNameList();
        System.out.println(name);
        System.out.println(peo);
        return peo;
    }

    @GetMapping("test2")
    public List<Company> test2() {
        List<Company> companyList = exImporter.convert("./企业人员汇总表.xlsx", "company", Company.class);
        System.out.println(companyList);
        return companyList;
    }

    @GetMapping("test3")
    public void test3(HttpServletResponse response) throws IOException {
        Company c = new Company();
        c.setAddress("address");
        c.setAmount(0.1245D);
        c.setDate(new Date());
        c.setMoney(0.23123);
        c.setName("yawn");
        c.setId(12);
        exExporter.export(Arrays.asList(c, c, c), "./export.xlsx");
        response.addHeader("Content-Disposition","attachment;filename=export.xlsx");
        exExporter.export(Arrays.asList(c, c, c), response.getOutputStream());
    }

    @GetMapping("test5")
    public void test5(HttpServletResponse response) throws IOException {
        Person person = createPerson();
        response.addHeader("Content-Disposition","attachment;filename=grade.xlsx");
        exTplExporter.export("成绩单模板.xlsx", person,
                response.getOutputStream(), "grade");
    }

    @GetMapping("test6")
    public Person test6() {
        return createPerson();
    }

    private Person createPerson() {
        Person person = new Person();
        person.setAddress("甘肃省静宁县");
        person.setAge(18);
        person.setName("西门吹水");
        person.setGender("男");
        List<GradeItem> gradeList = new ArrayList<>();
        gradeList.add(new GradeItem("语文", 91));
        gradeList.add(new GradeItem("数学", 92));
        gradeList.add(new GradeItem("English", 93));
        gradeList.add(new GradeItem("历史", 90));
        person.setGradeList(gradeList);
        person.setTotalGrade(new GradeItem("总分", 366));
        person.setDate(new Date());
        return person;
    }
}
